package com.example.jh352160.surfingtv.network.retrofit;

import okhttp3.OkHttpClient;
import retrofit2.Retrofit;
import retrofit2.converter.gson.GsonConverterFactory;

/**
 * Created by jh352160 on 2016/8/4
 */

public class CommonRetrofit {

    private static final String BASE_URL = "http://app4tv.sudaotech.com/platform/";
    private static Retrofit retrofit;

    public static Retrofit getRetrofit() {


//        OkHttpClient client=new OkHttpClient.Builder().cookieJar(new CookieJar() {
//
//            private final HashMap<String,List<Cookie>> cookieStore=new HashMap<String, List<Cookie>>();
//
//            @Override
//            public void saveFromResponse(HttpUrl url, List<Cookie> cookies) {
//                cookieStore.put(url.host(),cookies);
//            }
//
//            @Override
//            public List<Cookie> loadForRequest(HttpUrl url) {
//                List<Cookie> cookies=cookieStore.get(url.host());
//                return cookies !=null?cookies:new ArrayList<Cookie>();
//            }
//        }).build();

        //OkHttpClient okHttpClient = new OkHttpClient();
        OkHttpClient.Builder builder = new OkHttpClient.Builder();
        builder.addInterceptor(new AddCookiesInterceptor()).addInterceptor(new ReceivedCookiesInterceptor());
        builder.addInterceptor(new AddCookiesInterceptor()).addInterceptor(new AddCookiesInterceptor());
        //okHttpClient.interceptors().add(new AddCookiesInterceptor());
        //okHttpClient.interceptors().add(new ReceivedCookiesInterceptor());

        if (retrofit == null) {
            retrofit = new Retrofit.Builder()
                    .baseUrl(BASE_URL)
                    .addConverterFactory(GsonConverterFactory.create())
                    .client(builder.build())
                    .build();
        }
        return retrofit;
    }
}

